/**
*      author: Y_Yao
*      created: 2025-05-10 11:03:05
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 3e5 + 7; 
int n;
int a[N];
int b[N];

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
    }
    for(int i = 1;i <= n;i++)
    {
        cin >> b[a[i]];
    }

    vector<int> g;
    for(int i = 1;i <= n;i++)
    {
        int cur = b[i];
        auto it = lower_bound(g.begin(),g.end(),cur);
        if(it == g.end()) g.push_back(cur);
        else *it = cur;
    }

    cout << g.size() + n << endl;;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}